package net

import "sync"

type Session struct {
	sync.Mutex
	Uid  string
	Cid  string
	data map[string]any
}

/*
*
实例化session
*/
func NewSession(cid string) *Session {
	return &Session{
		Cid: cid,
	}
}

/*
*
设置属性
*/
func (s *Session) Put(key string, value string) {
	s.Lock()
	defer s.Unlock()
	s.data[key] = value
}

/*
*
获取值
*/
func (s Session) Get(key string) (any, bool) {
	s.Lock()
	defer s.Unlock()
	a, ok := s.data[key]
	return a, ok
}

/*
*
知道当前的uid,设置其session的值
*/
func (s *Session) SetData(uid string, data map[string]any) {
	s.Lock()
	defer s.Unlock()
	if s.Uid == uid {
		for k, v := range data {
			s.data[k] = v
		}
	}
}
